<script setup lang="tsx">
import { useTable } from '@/hooks/web/useTable'
import { useEventBus } from '@/hooks/event/useEventBus'
import { useI18n } from '@/hooks/web/useI18n'
import { type CurdData } from '@/api/warehouse/types'
import { type ActionButtonsTemplate } from '@/api/global/types'
import useSchema from './components/hooks/useSchema'
import { AllSchemas, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { BaseButton } from '@/components/Button'
import { ContentWrap } from '@/components/ContentWrap'
import { Table } from '@/components/Table'
import { delParameterConfigApi, getParameterConfigApi } from '@/api/system'
import { ElMessage, ElMessageBox } from 'element-plus'
import ParameterConfigDialog from './components/ParameterConfigDialog.vue'

const { t } = useI18n()
const { push } = useRouter()

const parameterConfigDialogRef = ref()

const allSchemas = ref<AllSchemas>({
  tableColumns: []
})

const { tableRegister, tableState, tableMethods } = useTable({
  immediate: true,
  fetchDataApi: async () => {
    const { currentPage, pageSize } = tableState

    const res = await getParameterConfigApi({
      pageIndex: unref(currentPage),
      pageSize: unref(pageSize)
    })

    return {
      list: res.data.list,
      total: res.data.total
    }
  }
})

const { loading, dataList, total, currentPage, pageSize } = tableState
const { getList } = tableMethods

useEventBus({
  name: 'getList',
  callback: (type: string) => {
    if (type === 'add') {
      currentPage.value = 1
    }
    getList()
  }
})

const action = (type: string, row?: CurdData) => {
  nextTick(() => {
    parameterConfigDialogRef.value.dialogVisible = true
    parameterConfigDialogRef.value.getBackShow(type, row)
  })
}

const delData = (row: CurdData) => {
  ElMessageBox.confirm(t('purchase.logistics.confirmDeletion'), t('purchase.logistics.tips'), {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      await delParameterConfigApi(row.id as number)
      ElMessage.success(t('purchase.logistics.deleteSuccess'))
      await getList()
    })
    .catch(() => {})
}
const actionButtonsTemplate: ActionButtonsTemplate = {
  length: 3,
  template: (data: any) => {
    // <BaseButton size="small" type="danger" onClick={() => delData(data.row)}>
    //   {t('action.del')}
    // </BaseButton>
    return (
      <>
        <BaseButton type="primary" size="small" onClick={() => action('edit', data.row)}>
          {t('action.edit')}
        </BaseButton>
      </>
    )
  }
}

const { crudSchemas } = useSchema({ actionButtonsTemplate })
const { allSchemas: newSchemas } = useCrudSchemas(crudSchemas)
allSchemas.value = {
  tableColumns: newSchemas.tableColumns || []
}
</script>

<template>
  <ContentWrap>
    <div class="mb-10px">
      <BaseButton type="primary" @click="action('add')">{{ t('action.add') }}</BaseButton>
    </div>

    <Table
      v-model:pageSize="pageSize"
      v-model:currentPage="currentPage"
      :columns="allSchemas.tableColumns"
      :data="dataList"
      :loading="loading"
      :pagination="{
        total: total
      }"
      @register="tableRegister"
    />
    <ParameterConfigDialog ref="parameterConfigDialogRef" @refresh="getList" />
  </ContentWrap>
</template>
